package com.example.administrator.runbook.findfragment.audiobooks;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import com.example.administrator.runbook.R;
import com.example.administrator.runbook.findfragment.adapter.AudioBooksDetailAdapter;
import com.example.administrator.runbook.findfragment.model.AudioBooksDetail;
import com.example.administrator.runbook.findfragment.network.GetAudioBooksDetailService;
import com.example.administrator.runbook.fragments.CommonFragment;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import rx.schedulers.Schedulers;

/**
 * A simple {@link Fragment} subclass.
 */
public class AbCityFragment extends CommonFragment implements SwipeRefreshLayout.OnRefreshListener, AdapterView.OnItemClickListener {

    private ListView mDetailsList;
    private List<AudioBooksDetail.Details> mCityDetailNewest = new ArrayList<>();
    private ImageView mLoading;
    private SwipeRefreshLayout mRefreshLayout;
    private Retrofit mRetrofit;
    private boolean isRequesting;

    public AbCityFragment() {
        // Required empty public constructor
    }

    @Override
    public String getTitle() {
        return "都市言情";
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View ret = inflater.inflate(R.layout.fragment_ab_city, container, false);

        ret = initView(ret);

        mRefreshLayout.setOnRefreshListener(this);

        rotateAnim();

        Retrofit.Builder builder = new Retrofit.Builder();
        builder.baseUrl("http://api.ximalaya.com/openapi-gateway-app/v2/albums/");
        builder.addConverterFactory(GsonConverterFactory.create());
        builder.addCallAdapterFactory(RxJavaCallAdapterFactory.createWithScheduler(Schedulers.newThread()));
        mRetrofit = builder.build();

        if (mRefreshLayout != null) {
            getData(mRetrofit);
        }

        return ret;
    }

    private View initView(View ret) {
        mRefreshLayout = (SwipeRefreshLayout) ret.findViewById(R.id.srl_ab_city);
        mDetailsList = (ListView) ret.findViewById(R.id.city_newest);
        mLoading = (ImageView) ret.findViewById(R.id.ab_ic_loading);
        return ret;
    }

    private void getData(Retrofit retrofit) {
        isRequesting = true;
        GetAudioBooksDetailService getAudioBooksDetailService = retrofit.create(GetAudioBooksDetailService.class);
        Call<AudioBooksDetail> cityDetails = getAudioBooksDetailService.getCityDetails();
        cityDetails.enqueue(new Callback<AudioBooksDetail>() {
            @Override
            public void onResponse(Call<AudioBooksDetail> call, Response<AudioBooksDetail> response) {
                if (response.isSuccessful()) {
                    AudioBooksDetail body = response.body();
                    mCityDetailNewest = body.getCityDetailNewest();
                    refreshListView(mCityDetailNewest);
                    if (mRefreshLayout.isRefreshing()) {
                        mRefreshLayout.setRefreshing(false);
                    }
                    isRequesting = false;
                }
            }

            @Override
            public void onFailure(Call<AudioBooksDetail> call, Throwable t) {
            }
        });
    }

    private void refreshListView(List<AudioBooksDetail.Details> cityDetailNewest) {
        AudioBooksDetailAdapter adapter = new AudioBooksDetailAdapter(getContext(), cityDetailNewest);
        mDetailsList.setAdapter(adapter);
        adapter.notifyDataSetChanged();
        mDetailsList.setOnItemClickListener(this);
        mLoading.setVisibility(View.INVISIBLE);
        mLoading.clearAnimation();
    }

    public void rotateAnim() {
        mLoading.setVisibility(View.VISIBLE);
        Animation anim = new RotateAnimation(0f, 360f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        anim.setFillAfter(true); // 设置保持动画最后的状态  
        anim.setDuration(3000); // 设置动画时间
        anim.setInterpolator(new AccelerateInterpolator()); // 设置插入器
        mLoading.startAnimation(anim);
    }

    @Override
    public void onRefresh() {
        if (!isRequesting) {
            getData(mRetrofit);
        } else {
            if (mRefreshLayout.isRefreshing()) {
                mRefreshLayout.setRefreshing(false);
            }
            Toast.makeText(getContext(), "网络开小差了", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onPause() {
        if (mRefreshLayout.isRefreshing()) {
            mRefreshLayout.setRefreshing(false);
        }
        super.onPause();
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Toast.makeText(this.getContext(), "" + position, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onDestroy() {

        super.onDestroy();
    }
}
